<?php
/*
 * program:	均源
 * author:	雨中漫步 & 上善若水 & qhxin
 * email:	qhxin@qhxin.com
 * date:	2014/11/11
 * description:	又是一年双十一，不能剁手，就写点东西吧~~~~~~
 */

defined('IN_SCHOOL') or die();

/*{TranslateTagBegin}*/
class control_wechat extends base_control{

	/* 微信公众号后台需配置api接口地址：index.php?wechat-index.htm */
	
	//hook ctr_wechat_before.php

	public function __construct() {
	
		//hook ctr_wechat_parent_init_before.php
		parent::__construct();
		
		//hook ctr_wechat_parent_init_after.php
	}
	
	public function do_index() {
		//hook ctr_wechat_index_start.php
		define('APP_OUT_JSON',1);
		
		$jywx_options = array(
			//基本配置：token,appid,appsecret
			'token'=>'','appid'=>'','appsecret'=>'',
			//关注消息：微信名称,微信描述,微信图片,跳转页面
			'jywxname'=>'', 'jywxdes'=>'','jywxpic'=>'', 'jywxurl'=>''
		);
		//hook ctr_wechat_index_get_options_start.php
		$this->get_option($jywx_options);
		
		//hook ctr_wechat_index_init_options_start.php
		$this->wx->init_options($jywx_options);
		$this->wx->valid();
		//hook ctr_wechat_index_valid_after.php
		$this->wx->getRev();
		$fromUsername = $this->wx->getRevFrom();		//微信用户id，已加密
		$toUsername = $this->wx->getRevTo();			//消息目标账户id
		$keyword = trim($this->wx->getRevContent());	//用户消息
		$time = $_SERVER['time'];
		$type = $this->wx->getRevType();				//消息类型
		$contentStr = '';
		$reply_array = array(
					'ToUserName' => $fromUsername,
					'FromUserName' =>  $toUsername,
					'CreateTime' => $time,
					'MsgType' => 'news',
					'ArticleCount'=> 1,
					'Articles'=> array(
						'item'=>array(
							'Title'=> ('欢迎您关注'.$jywx_options['jywxname'] ),
							'Description'=> $jywx_options['jywxdes'],
							'PicUrl'=> $jywx_options['jywxpic'],
							'Url'=> $jywx_options['jywxurl'])
					),
					'FuncFlag'=>1
				);
		//hook ctr_wechat_index_switch_before.php
		switch($type) {
		case Wechat::MSGTYPE_TEXT:
		/* 关键字查询，这里仅对帖子进行搜索 */
			//hook ctr_wechat_index_in_case_text_start.php
			$keyword_res = $this->talks->talks_search_list($keyword, 0 , 1);
			if(!empty($keyword_res)) {
				foreach($keyword_res as $the_talk) {
					$reply_array['Articles']['item']['Title'] = $the_talk['title'];
					$reply_array['Articles']['item']['Description'] = $this->parse_content($the_talk['content']);
					$reply_array['Articles']['item']['PicUrl'] = '';
					$reply_array['Articles']['item']['Url'] = 'http://'.CONFIG_APP_HOST.'/index.php?index-talk-tid-'.$the_talk['tid'].'.htm';
				}
				//hook ctr_wechat_index_in_case_text_before_reply.php
				$this->wx->reply($reply_array);
			}else{
				$this->wx->text("找不到此关键字记录！")->reply();
			}
			break;
		case Wechat::MSGTYPE_EVENT:
			//hook ctr_wechat_index_switch_in_case_event.php
			$this->wx->reply($reply_array);
			break;
		//hook ctr_wechat_index_switch_in_case.php
		default:
			$this->wx->text('2015 JYWAP @ jy.qhxin.com')->reply();
		}
	}
	
	//=============>private
	private function parse_content($c) {
		//hook ctr_wechat_index_parse_content.php
		$c = str_replace("\n", "\n\n", $c);
		$c = utf8::cutstr_cn($c, 300, "\n\n[点击查看更多]");
		return $c;
	}
	
	private function get_option(&$options) {
		//hook ctr_wechat_index_get_option.php
		$config_path  = CONFIG_PATH . 'config_wechat.json';
		!is_file($config_path) && file_put_contents($config_path, core::json_encode($options), LOCK_EX);
		$options = core::json_decode(file_get_contents($config_path));
	}
	
	//hook ctr_wechat_end.php
}
/*{TranslateTagEnd}*/

?>